/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.ui.wizard;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.dltk.core.environment.IEnvironment;
import org.eclipse.dltk.launching.IInterpreterInstall;
import org.eclipse.dltk.ui.wizards.IProjectWizard;
import org.eclipse.dltk.ui.wizards.IProjectWizardPage;
import org.eclipse.dltk.ui.wizards.NewElementWizard;
import org.eclipse.dltk.ui.wizards.ProjectCreator;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;

import com.funkyroach.use.core.language.USNature;

public class USNewProjectWizard extends NewElementWizard implements INewWizard, IExecutableExtension, IProjectWizard {
    public static final String WIZARD_ID = null;
    
    private USNewProjectWizardFirstPage firstPage;
    private USNewProjectWizardSecondPage secondPage;
    private IConfigurationElement fConfigElement;

    @Override
    public void addPages() {
        super.addPages();
        firstPage = new USNewProjectWizardFirstPage();
        secondPage = new USNewProjectWizardSecondPage();

        addPage(firstPage);
        addPage(secondPage);

    }

    @Override
    public void createPageControls(Composite pageContainer) {
        initProjectWizard();
        super.createPageControls(pageContainer);
    }

    /**
     * @since 3.0
     */
    protected void initProjectWizard() {
        for (IWizardPage page : getPages()) {
            if (page instanceof IProjectWizardPage) {
                ((IProjectWizardPage) page).initProjectWizardPage();
            }
        }
    }

    public IProject getProject() {
        return firstPage.getProjectHandle();
    }

    public void createProject() {
        updateSteps(getContainer().getCurrentPage());
        getProjectCreator().changeToNewProject();
    }

    public void removeProject() {
        getProjectCreator().removeProject();
    }

    public boolean performCancel() {
        getProjectCreator().removeProject();
        return super.performCancel();
    }

    private ProjectCreator fCreator;

    public ProjectCreator getProjectCreator() {
        if (fCreator == null) {
            fCreator = createProjectCreator();
        }
        return fCreator;
    }

    protected ProjectCreator createProjectCreator() {
        return new USProjectCreator(this, firstPage);
    }

    protected void updateSteps(final IWizardPage currentPage) {
        for (IWizardPage page : getPages()) {
            if (page == currentPage) {
                break;
            }
            if (page instanceof IProjectWizardPage) {
                ((IProjectWizardPage) page).updateProjectWizardPage();
            }
        }
    }

    public IScriptProject getCreatedElement() {
        return secondPage.getScriptProject();
    }

    @Override
    public boolean performFinish() {
        updateSteps(null);
        boolean res = super.performFinish();
        if (res) {
            final IScriptProject newElement = getCreatedElement();
            // IWorkingSet[] workingSets = ((ProjectWizardFirstPage)
            // getFirstPage())
            // .getWorkingSets();
            // if (workingSets.length > 0) {
            // getWorkbench().getWorkingSetManager().addToWorkingSets(
            // newElement, workingSets);
            // }
            BasicNewProjectResourceWizard.updatePerspective(fConfigElement);
            selectAndReveal(newElement.getProject());
        }
        return res;
    }

    public void setInitializationData(IConfigurationElement cfig, String propertyName, Object data) {
        fConfigElement = cfig;
    }

    @Override
    public boolean isEnabledPage(IWizardPage page) {
        return true;
    }

    @Override
    public IEnvironment getEnvironment() {
        return null;
    }

    @Override
    public IInterpreterInstall getInterpreter() {
        return null;
    }

    @Override
    protected void finishPage(IProgressMonitor monitor) throws InterruptedException, CoreException {
        getProjectCreator().performFinish(monitor);
    }

    public String getScriptNature() {
        return USNature.NATURE_ID;
    }


}
